<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="../../scripts/boot.js" type="text/javascript"></script>
  <script src="../../demo/demo.css" type="text/javascript"></script>
  <style type="text/css">
    html, body
    {        
        padding:0;
        margin:0;
        border:0;
        height:100%;
        overflow:hidden;
    }
    </style>
</head>
<body>
  <form id="form1" method="post">
    <!-- <mini-hidden name="clubId"></mini-hidden> -->
    <fieldset style="border:solid 1px #aaa;padding:3px;">
        <legend >新增活动信息</legend>
        <div style="padding:5px;">
    <table>
        <tr>
            <td align="right">俱乐部名称：</td>
            <td>    
                <input name="clubId" class="mini-combobox"  
                url="/club/clublistaction?method=queryAllClubInfo"
                valueField="clubId" textField="clubName"
                onValueChanged="onClubValueChanged"
                required="true" />
            </td>
            <td  align="right">活动编号：</td>
            <td >
                <input id="idActivityId" name="activityId" class="mini-textbox" required="true"/> 
            </td>
            
        </tr>
        <tr>
          <td  align="right">活动名称：</td>
            <td >    
                <input name="activityName" class="mini-textbox" required="true"/>
            </td>
            <td  align="right">举办时间：</td>
            <td >
              <input id="idDateTime" name="publicTime" class="mini-datepicker" nullValue="null"
                format="yyyy-MM-dd HH:mm" timeFormat="HH:mm"  required="true" ondrawDate="onDrawDate"
                showTime="true" showOkButton="true" showClearButton="false"/>
            </td>
            
        </tr>   
        <tr>
            <td  align="right">举办地点：</td>
            <td  colspan="3">
              <div name="publicPlace" class="mini-radiobuttonlist" repeatItems="1"
               repeatDirection="vertical" repeatLayout="table" required="true"
               url="../../data/place.txt" textField="text" valueField="id">
              </div>
            </td>
        </tr> 
        <tr>
            <td align="right">免门票：</td>
            <td >  
              <div name="isFree" class="mini-radiobuttonlist" 
              repeatItems="1" url="../../data/free.txt"  value="2"
              textField="text" valueField="id"  required="true"></div>
            </td>
        </tr> 
        <tr>
          <td align="right">报名人数：</td>
          <td >  
            <input name="partNum" class="mini-spinner"  required="true" minValue="0" maxValue="500"/>
          </td>
      </tr>          
    </table>            
        </div>
    </fieldset>
    <div style="text-align:center;padding:10px;">               
        <a class="mini-button" onclick="onOk" style="width:60px;margin-right:20px;">确定</a>       
        <a class="mini-button" onclick="onCancel" style="width:60px;">取消</a>           
    </div>        
</form>
<script type="text/javascript">
    mini.parse();

    var form = new mini.Form("form1");
    var dateTime = mini.get('idDateTime')
    dateTime.setValue(new Date())

    // 查询所有俱乐部信息
    queryAllClubInfo()
    var remainScore = 0;

//////////////////////////////////////////////////////////
    function onDrawDate(e) {
        var date = e.date;
        var d = new Date();

        if (date.getTime() < d.getTime()) {
            e.allowSelect = false;
        }
    }



/////////////////////////////////////////////////////////////

    function SaveData() {
        var o = form.getData();            

        form.validate();
        if (form.isValid() == false) {
            return;
        }

        if (o.publicTime.getTime() < (new Date).getTime()) {
          mini.alert('举办时间不能早于当前时间', '提示')
          return ;
        }

        if (o.partNum > remainScore) {
          mini.alert('报名人数不能多于余票数', '提示')
          return ;
        }

        var json = mini.encode(o);
        console.log('SaveData', json)
        addActivityInfo(json)

        
    }

    function onOk(e) {
        SaveData();
    }
    function onCancel(e) {
        CloseWindow("cancel");
    }

    //////////////////ajax/////////////////////////////////
    // 查询所有俱乐部信息
    function queryAllClubInfo() {
      $.ajax({
            url: "/club/clublistaction?method=queryAllClubInfo",
            type: 'GET',
            cache: false,
            success: function (res) {
              // 一定要转一下不然不是对象形式
              var o = mini.decode(res)
              console.log('queryAllClubInfo', o)
            },
            error: function () {
            }
        });
    }

    // 请求活动编号
    function getActivityId(clubId, clubType) {
      $.ajax({
            url: "/club/activitylistaction?method=getActivityId&clubId=" + clubId + "&clubType=" + clubType,
            type: 'GET',
            cache: false,
            success: function (res) {
              mini.get('idActivityId').setValue(mini.decode(res))
            },
            error: function () {
            }
        });
    }

    // 新增活动信息
    function addActivityInfo(json) {
      $.ajax({
            url: "/club/activityaddaction?method=addActivityInfo",
            type: 'POST',
            data: {data: json},
            cache: false,
            success: function (text) {
              mini.alert(text, '提示', function() {
                CloseWindow('save')
              })
            },
            error: function () {
            }
        });
    }

    ////////////////事件////////////////////////
    function onClubValueChanged(e) {
      console.log('changed', e.selected)
      // 记录所选的俱乐部剩余票数
      remainScore = e.selected.remain
      // 请求活动编号
      getActivityId(e.selected.clubId, e.selected.clubType)
    }

    // 管理窗口
    function CloseWindow(action) {            
      window.CloseOwnerWindow(action);       
    }

</script>
</body>
</html>